/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.StopsIntentHolder;
import lt.marsrutai.mobile.android.model.Stop;
import lt.marsrutai.mobile.android.model.TransportStopBookmark;
import lt.marsrutai.mobile.android.util.BookmarkControlManager;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


public class StopAdapter extends ListViewAdapter<Stop> {

    private Bundle fragmentState;
    
    public StopAdapter(Activity activity, Bundle fragmentState) {
        super(activity, R.layout.stops_item);
        this.fragmentState = fragmentState;
    }
    
    public void setStops(List<Stop> stops) {
        if (stops != null && stops.size() > 0) {
            for (int i = 0; i < stops.size(); i++) {
                add(stops.get(i));
            }
        }
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {
        final Stop stop = getItem(position);
        final StopsIntentHolder holder = new StopsIntentHolder(fragmentState);

        if (stop != null) {
            TextView stopName = (TextView) view.findViewById(R.id.stop_name);
            if (stopName != null) {
                stopName.setText(stop.getName());
            }

            ImageButton mapButton = (ImageButton) view.findViewById(R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StopMapIntentHolder intentHolder = new StopMapIntentHolder(activity);
                    intentHolder.setScopeFolder(holder.getScopeFolder());
                    intentHolder.setStopId(stop.getId());
                    intentHolder.setTrackId(holder.getTrackId());
                    intentHolder.setScheduleName(holder.getScheduleName());
                    intentHolder.setTransportId(holder.getTransportId());
                    intentHolder.setTitle(holder.getTrackName());
                    activity.startActivity(intentHolder.getIntent());
                }
            });

            final TransportStopBookmark stopBookmark = new TransportStopBookmark();
            stopBookmark.setScopeFolder(holder.getScopeFolder());
            stopBookmark.setScopeName(holder.getScopeName());
            stopBookmark.setStopId(stop.getId());
            stopBookmark.setStopName(stop.getName());
            stopBookmark.setStopDirectionName(stop.getDirectionName());
            stopBookmark.setTransportId(holder.getTransportId());
            stopBookmark.setScheduleId(holder.getScheduleId());
            stopBookmark.setScheduleName(holder.getScheduleName());
            stopBookmark.setScheduleTrackName(holder.getScheduleTrackName());
            stopBookmark.setTrackId(holder.getTrackId());
            stopBookmark.setTrackName(holder.getTrackName());
            
            BookmarkControlManager.setUpControls(view.findViewById(R.id.star_on), view.findViewById(R.id.star_off), stopBookmark);
        }
        return view;
    }
}
